﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.SessionState;
using System.Data;
using System.IO;

using Newtonsoft.Json;
using Framework.DataAccess;
using Framework.Common.JsonEntities;
using Framework.Common;
using Framework.Common.Common;
using Mode;

namespace WebSite.WebServer
{
    /// <summary>
    /// MainAdmin 的摘要说明
    /// </summary>
    public class MainAdmin : IHttpHandler,IRequiresSessionState
    {
        JsonReturnMessages msg;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            msg = new JsonReturnMessages();
            try
            {
                if (context.Request.Params["Action"] == "isLogin")
                    isLogin();

                if (context.Request.Params["Action"] == "RegAudit")
                    RegAuditList();
                if (context.Request.Params["Action"] == "AuditItem")
                    AuditItem();
                if (context.Request.Params["Action"] == "DeleteItem")
                    DeleteItem();
                if (context.Request.Params["Action"] == "RoleTree")
                    RoleTree();
                if (context.Request.Params["Action"] == "AreaRolUserList")
                    AreaRolUserList();
                if (context.Request.Params["Action"] == "AreaTree")
                       GetTheAreaTree();
                if(context.Request.Params["Action"] =="UpdateUser")
                    UpdateUser();
                if (context.Request.Params["Action"] == "UpdatePwd")
                    UpdatePwd();
                if (context.Request.Params["Action"] == "AreaRoleList")
                    AreaRoleList();
                if (context.Request.Params["Action"] == "UpdateRole")
                      UpdateRole();
                if (context.Request.Params["Action"] == "AddRole")
                     AddRole();
                if (context.Request.Params["Action"] == "RoleList")
                    RoleList();
                if (context.Request.Params["Action"] == "AddRoleNew")
                       AddRoleNew();
                if (context.Request.Params["Action"] == "UpdateRoleNew")
                    UpdateRoleNew();
                if (context.Request.Params["Action"] == "DeleteRoleItem")
                     DeleteRoleItem();
                if (context.Request.Params["Action"] == "GetTheRoleTableList")
                       GetTheRoleTableList();
                if (context.Request.Params["Action"] == "getUserInfro")
                    GetUserInfro();
                if (context.Request.Params["Action"] == "LoginOut")
                    LoginOut();
                if (context.Request.Params["Action"] == "GetSystem")
                    GetSystem();
                if (context.Request.Params["Action"] == "SetSystem")
                    SetSystem();
                if (context.Request.Params["Action"] == "DeleteUser")
                    DeleteUser();
                
                
                
            }
            catch (Exception err)
            {
                var message = err.Message;
                if (err.InnerException != null && err.InnerException.Message != null)
                    message += "<BR>" + err.InnerException.Message;

                msg.IsSuccess = 0;
                msg.Msg = message;

                var json = JsonConvert.SerializeObject(msg);
                context.Response.Write(json);
            }
            context.Response.End();
        }
        /// <summary>
        /// comobox赋值
        /// </summary>
        void GetTheRoleTableList()
        {
            var context = System.Web.HttpContext.Current;
               loginInfo lu = chklogin();
               if (lu != null)
               {
                   role myRole = new role();
                    IList<role_table> list;
                   if(lu.R_POWER ==1)
                      list = myRole.GetRolist(false);
                   else
                       list = myRole.GetRolist(true);
                   string str = "";
                   foreach (role_table Item in list)
                   {
                       str += "{\"id\":\"" + Item.id + "\",\"text\":\"" + Item.name + "\",\"power\":\""+Item.power+"\"},";
                   }
                   str = str.TrimEnd(',');
                   context.Response.Write("[" + str + "]");
               }else
               context.Response.Write("[]");
        }
        void DeleteRoleItem()
        {
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                msg.IsSuccess = 0;
                if (lu.R_POWER == 1)
                {
                    int  id = context.Request.Params["id"].ToInt();
                   
                    role myrl = new role();
                    int R = myrl.DeleteRoleItem(id);
                    if (R > 0)
                    {
                        msg.IsSuccess = 1;
                        msg.Msg = "删除成功";
                    }
                    else
                    {
                        msg.IsSuccess = R;
                        msg.Msg = "删除失败！";
                    }
                }
                else
                {
                    msg.Msg = "你没权限操作！";
                }
            }
            context.Response.Write(JsonConvert.SerializeObject(msg));
        }
        /// <summary>
        /// 修改
        /// </summary>
        void UpdateRoleNew()
        {
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                msg.IsSuccess = 0;
                if (lu.R_POWER == 1)
                {
                    string pdata = context.Request.Params["data"];
                    areroleview mbItem = JsonConvert.DeserializeObject<areroleview>(pdata);
                    role myrl = new role();
                    int R = myrl.UpdateRoleNew(mbItem);
                    if (R > 0)
                    {
                        msg.IsSuccess = 1;
                        msg.Msg = "修改成功";
                    }
                    else
                    {
                        msg.IsSuccess = R;
                        msg.Msg = "修改失败！";
                    }
                }
                else
                {
                    msg.Msg = "你没权限操作！";
                }
            }
            context.Response.Write(JsonConvert.SerializeObject(msg));
           
        }
        
        /// <summary>
        /// 添加角色
        /// </summary>
        void AddRoleNew()
        { 
             var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                msg.IsSuccess = 0;
                if (lu.R_POWER == 1)
                {
                    string pdata = context.Request.Params["data"];
                    areroleview mbItem = JsonConvert.DeserializeObject<areroleview>(pdata);
                    role myrl = new role();
                    int R =myrl.AddRoleNew(mbItem);
                    if (R > 0)
                    {
                        msg.IsSuccess = 1;
                        msg.Msg = "添加成功";
                    }
                    else
                    {
                        msg.IsSuccess = R;
                        msg.Msg = "添加失败！";
                    }
                }
                else
                {
                    msg.Msg = "你没权限操作！";
                }
            }
            context.Response.Write(JsonConvert.SerializeObject(msg));
            
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        void AddRole()
        {
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                msg.IsSuccess = 0;
                if (lu.R_POWER == 1)
                {
                    string pdata = context.Request.Params["data"];
                    areroleview mbItem = JsonConvert.DeserializeObject<areroleview>(pdata);
                    role myrl = new role();

                    int R =myrl.AddRole(mbItem);

                    if ( R> 0)
                    {
                        msg.IsSuccess = 1;
                        msg.Msg = "添加成功";
                    }
                    else
                    {
                        msg.IsSuccess = R;
                        msg.Msg = "添加失败！";
                    }
                }
                else
                {
                    msg.Msg = "你没权限操作！";
                }
                context.Response.Write(JsonConvert.SerializeObject(msg));
            }
        }
        void RoleList()
        {
            var context = System.Web.HttpContext.Current;
            PageView pageInfo = new PageView();
            pageInfo.PageIndex = context.Request.Params["page"].ToInt(1);
            pageInfo.PageSize = context.Request.Params["pagesize"].ToInt(20);
            pageInfo.SortName = context.Request.Params["sortname"].ToStr();
            pageInfo.SortOrder = context.Request.Params["sortorder"].ToStr();
            pageInfo.SearchParams = context.Request.Params;
            role myRole = new role();

            PageList<object> List = myRole.GetRolist(pageInfo,false);
            string url = "";

            string Rjson = @"{""Rows"":null,""Total"":""0"",""Url"":""""}";
            if (List.Data != null)
            {
                Rjson = @"{""Rows"":" + List.Data.ToJson() + @",""Total"":""" + List.Total + @""",""Url"":""" + url + @"""}";
            }


            context.Response.Write(Rjson);


        
        }

        /// <summary>
        /// 修改权限
        /// </summary>
        void UpdateRole()
        {
            var context = System.Web.HttpContext.Current;
             loginInfo lu = chklogin();
             if (lu != null)
             { 
                msg.IsSuccess = 0;
                if (lu.R_POWER == 1)
                {
                    string pdata = context.Request.Params["data"];
                    areroleview mbItem = JsonConvert.DeserializeObject<areroleview>(pdata);
                    role myrl = new role();

                    int R = myrl.UpdateRole(mbItem);
                    if (R > 0)
                    {
                        msg.IsSuccess = 1;
                        msg.Msg = "修改成功";
                    }
                    else
                    {
                        msg.IsSuccess = R;
                        msg.Msg = "修改失败！";
                    }
                }
                else
                {
                    msg.Msg = "你没权限操作！";
                }
                context.Response.Write(JsonConvert.SerializeObject(msg));
             }
        }
        /// <summary>
        /// 区域下的角色列表
        /// </summary>
        void AreaRoleList()
        {
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
              
                role myRole = new role();

                int areid = context.Request.Params["aid"].ToInt();
                int status = context.Request.Params["status"].ToInt();
                string rolename = context.Request.Params["roleName"].ToString();
                   
                PageView pageInfo = new PageView();
                pageInfo.PageIndex = context.Request.Params["page"].ToInt(1);
                pageInfo.PageSize = context.Request.Params["pagesize"].ToInt(20);
                pageInfo.SortName = context.Request.Params["sortname"].ToStr();
                pageInfo.SortOrder = context.Request.Params["sortorder"].ToStr();
                pageInfo.SearchParams = context.Request.Params;


                PageList<object> List = myRole.GetRolist(areid, status, rolename, pageInfo);


                string Rjson = @"{""Rows"":null,""Total"":""0""}";
                if (List.Data != null)
                {
                    Rjson = @"{""Rows"":" + List.Data.ToJson() + @",""Total"":""" + List.Total + @"""}";
                }


                context.Response.Write(Rjson);


            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        void UpdatePwd()
        {
            var context = System.Web.HttpContext.Current;

            loginInfo lu = chklogin();
            if (lu != null)
            {


                msg.IsSuccess = 0;
                if (lu.R_POWER == 1)
                {
                    try
                    {
                        int id=context.Request.Params["id"].ToInt(0);
                        string pwd = context.Request.Params["pwd"].Trim();

                        // 管理员改自己的密码
                        if (id == -1)
                            id = lu.M_ID;
                        

                        User u = new User();
                        if (u.UpdatePwd(id,pwd)> 0)
                        {
                            msg.IsSuccess = 1;
                            msg.Msg = "修改成功";
                        }
                        else
                        {
                            msg.Msg = "修改失败！";
                        }
                    }
                    catch (Exception er)
                    {

                        msg.Msg = er.Message;
                    }
                }
                else
                {
                    msg.Msg = "你没权限操作！";
                }
                context.Response.Write(JsonConvert.SerializeObject(msg));

            }
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        void UpdateUser()
        { 
             var context = System.Web.HttpContext.Current;
            
            loginInfo lu = chklogin();
            if (lu != null)
            {
               

                msg.IsSuccess = 0;
                if (lu.R_POWER == 1)
                {
                    try
                    {
                        string pdata = context.Request.Params["data"];
                        member_view mbItem = JsonConvert.DeserializeObject<member_view>(pdata);
                        User u = new User();


                        if (u.UpdateUserItem(mbItem) > 0)
                        {
                            msg.IsSuccess = 1;
                            msg.Msg = "修改成功";
                        }
                        else
                        {
                            msg.Msg = "修改失败！";
                        }
                    }
                    catch (Exception er)
                    {
                      
                        msg.Msg = er.Message;
                    }
                }
                else
                {
                   msg.Msg = "你没权限操作！";
                }
                context.Response.Write(JsonConvert.SerializeObject(msg));

            }
        }
        /// <summary>
        /// 角色下的用户
        /// </summary>
        void AreaRolUserList()
        {
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                string url = "";
                string fileName = context.Request.Params["fileName"].ToStr();

                url = "fileName=" + fileName;
                User u = new User();

                int areid = context.Request.Params["aid"].ToInt(0);
                int rolid = context.Request.Params["rid"].ToInt(0);
                int status = context.Request.Params["status"].ToInt(0);
                


                PageView pageInfo = new PageView();
                pageInfo.PageIndex = context.Request.Params["page"].ToInt(1);
                pageInfo.PageSize = context.Request.Params["pagesize"].ToInt(20);
                pageInfo.SortName = context.Request.Params["sortname"].ToStr();
                pageInfo.SortOrder = context.Request.Params["sortorder"].ToStr();
                pageInfo.SearchParams = context.Request.Params;

                url += "&aid=" + areid + "&rid=" + rolid + "&page=" + pageInfo.PageIndex + "&pagesize=" + pageInfo.PageSize + "&sortname=" + pageInfo.SortName + "&sortorder=" + pageInfo.SortOrder;
                url += "&userName=" + context.Request.Params["userName"].ToStr();


                PageList<object> List = u.GetTheRegUserList(areid,rolid, status, pageInfo);



                    string Rjson = @"{""Rows"":null,""Total"":""0"",""Url"":""""}";
                    if (List.Data != null)
                    {
                        Rjson = @"{""Rows"":" + List.Data.ToJson() + @",""Total"":""" + List.Total + @""",""Url"":""" + url + @"""}";
                    }


                    context.Response.Write(Rjson);

                
            }
        }

        /// <summary>
        /// 获得区域
        /// </summary>
        void GetTheAreaTree()
        {

            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                var nodes = new List<JsonTreeNode>();
                User myuser = new User();
                if (lu.A_LEVEL == 0)
                {
                    DataView dv = myuser.GetTheAreaList(0, 0);
                    if (dv != null)
                    {
                        DataView dvparent = dv.ToTable().DefaultView;
                        dvparent.RowFilter = "level=0";
                        JsonTreeNode node = null;
                        if (dvparent != null)
                        {
                            if (dvparent.Count > 0)
                            {
                                node = new JsonTreeNode();
                                node.id = dvparent[0]["id"].ToString();
                                node.text = dvparent[0]["name"].ToString();
                            }

                        }

                        DataView dvch = dv.ToTable().DefaultView;
                        dvch.RowFilter = "level=1";

                        if (dvch != null)
                        {
                            for (int j = 0; j < dvch.Count; j++)
                            {
                                if (node != null)
                                {
                                    JsonTreeNodeCh cnode = new JsonTreeNodeCh();
                                    cnode.id = dvch[j]["id"].ToString();
                                    cnode.text = dvch[j]["name"].ToString();

                                    node.children.Add(cnode);
                                }
                                else
                                {
                                    JsonTreeNode nodec = new JsonTreeNode();
                                    nodec.id = dvch[j]["id"].ToString();
                                    nodec.text = dvch[j]["name"].ToString();

                                    nodes.Add(nodec);
                                }
                            }
                        }
                        if (node != null) nodes.Add(node);

                    }//end dv
                }
                else
                {
                    DataView dv = myuser.GetTheAreaList(lu.A_LEVEL, lu.A_ID);
                    if (dv != null)
                    {
                        DataView dvparent = dv.ToTable().DefaultView;
                        dvparent.RowFilter = "level=0";
                        JsonTreeNode node = null;
                        if (dvparent != null)
                        {
                            if (dvparent.Count > 0)
                            {
                                node = new JsonTreeNode();
                                node.id = dvparent[0]["id"].ToString();
                                node.text = dvparent[0]["name"].ToString();
                            }

                        }

                        DataView dvch = dv.ToTable().DefaultView;
                        dvch.RowFilter = "level=1";

                        if (dvch != null)
                        {
                            for (int j = 0; j < dvch.Count; j++)
                            {
                                if (node != null)
                                {
                                    JsonTreeNodeCh cnode = new JsonTreeNodeCh();
                                    cnode.id = dvch[j]["id"].ToString();
                                    cnode.text = dvch[j]["name"].ToString();

                                    node.children.Add(cnode);
                                }
                                else
                                {
                                    JsonTreeNode nodec = new JsonTreeNode();
                                    nodec.id = dvch[j]["id"].ToString();
                                    nodec.text = dvch[j]["name"].ToString();

                                    nodes.Add(nodec);
                                }
                            }
                        }
                        if (node != null) nodes.Add(node);
                    }
                  
                }//end null
                context.Response.Write(JsonConvert.SerializeObject(nodes));
            }
        }
        /// <summary>
        /// 角色树
        /// </summary>
        void RoleTree()
        {

            var context = System.Web.HttpContext.Current;
            StringBuilder sb = new StringBuilder();
            loginInfo lu = chklogin();
            if (lu != null)
            {
                User myuser = new User();
                if (lu.A_LEVEL == 0)
                {
                    DataView dv = myuser.GetTheAreaList(0, 0);

                    if (dv != null)
                    {
                        DataView dvparent = dv.ToTable().DefaultView;
                        dvparent.RowFilter = "level=0";
                        string pnode = "";
                        //看顶级的Role
                        string topRolse = "";
                        if (dvparent != null)
                        {

                            if (dvparent.Count > 0)
                            {
                                pnode = @"{""id"":" + dvparent[0]["id"].ToString() + @",""text"":""" + dvparent[0]["name"].ToString() + @"""" + @",""areid"":""" + dvparent[0]["id"].ToInt() + @""",""value"":""" + "-1" + @"""";


                                DataView dv3 = myuser.GetTherAreaPowerRoleList(dvparent[0]["id"].ToInt(), 0);
                                for (int i = 0; i < dv3.Count; i++)
                                {
                                    topRolse += @"{""id"":" + dv3[i]["ID"].ToString() + @",""text"":""" + dv3[i]["NAME"].ToString() + @"""" + @",""areid"":""" + dvparent[0]["id"].ToInt() + @""",""value"":""" + dv3[i]["ID"].ToString() + @"""},";
                                }
                            }
                        }

                        DataView dvch = dv.ToTable().DefaultView;
                        dvch.RowFilter = "level=1";
                        string nArea = "";
                        if (dvch != null)
                        {
                            for (int j = 0; j < dvch.Count; j++)
                            {


                                string tempnArea = @"""id"":" + dvch[j]["id"].ToString() + @",""text"":""" + dvch[j]["name"].ToString() + @"""" + @",""areid"":""" + dvch[j]["id"].ToInt() + @""",""value"":""" + "-1" + @"""";


                                //找的该区域下角色
                                string Rolse = "";
                                DataView dv2 = myuser.GetTherAreaPowerRoleList(dvch[j]["id"].ToInt(), 0);
                                for (int k = 0; k < dv2.Count; k++)
                                {
                                    Rolse += @"{""id"":" + dv2[k]["ID"].ToString() + @",""text"":""" + dv2[k]["NAME"].ToString() + @"""" + @",""areid"":""" + dvch[j]["id"].ToInt() + @""",""value"":""" + dv2[k]["ID"].ToString() + @"""},";
                                }
                                nArea += "{" + tempnArea + @",""children"":[" + Rolse.TrimEnd(',') + "]},";
                            }


                        }
                        if (pnode != "")
                        {
                            pnode += "," + @"""children"":[" + nArea.TrimEnd(',') + "]}";

                            if (topRolse.TrimEnd(',') != "")
                                pnode += "," + topRolse.TrimEnd(',');
                        }
                        else
                            pnode = nArea.TrimEnd(',');


                        context.Response.Write("["+pnode+"]");



                    }//dv
                }
                else //不是老大的数
                {
                    DataView dv = myuser.GetTheAreaList(lu.A_LEVEL, lu.A_ID);
                    if (dv != null)
                    {
                        DataView dvparent = dv.ToTable().DefaultView;
                        dvparent.RowFilter = "level=0";
                        string pnode = "";
                        //看顶级的Role
                        string topRolse = "";
                        if (dvparent!=null)
                        {

                            if (dvparent.Count > 0)
                            {
                                pnode = @"{""id"":" + dvparent[0]["id"].ToString() + @",""text"":""" + dvparent[0]["name"].ToString() + @"""" + @",""areid"":""" + dvparent[0]["id"].ToInt() + @""",""value"":""" + "-1" + @"""";


                                DataView dv3 = myuser.GetTherAreaPowerRoleList(dvparent[0]["id"].ToInt(), 0);
                                for (int i = 0; i < dv3.Count; i++)
                                {
                                    topRolse += @"{""id"":" + dv3[i]["ID"].ToString() + @",""text"":""" + dv3[i]["NAME"].ToString() + @"""" + @",""areid"":""" + dvparent[0]["id"].ToInt() + @""",""value"":""" + dv3[i]["ID"].ToString() + @"""},";
                                }
                            }
                        }

                        DataView dvch = dv.ToTable().DefaultView;
                        dvch.RowFilter = "level=1";
                        string nArea = "";
                        if (dvch != null)
                        {
                            for (int j = 0; j < dvch.Count; j++)
                            {


                                string tempnArea = @"""id"":" + dvch[j]["id"].ToString() + @",""text"":""" + dvch[j]["name"].ToString() + @"""" + @",""areid"":""" + dvch[j]["id"].ToInt() + @""",""value"":""" + "-1" + @"""";


                                //找的该区域下角色
                                string Rolse = "";
                                DataView dv2 = myuser.GetTherAreaPowerRoleList(dvch[j]["id"].ToInt(), 0);
                                for (int k = 0; k < dv2.Count; k++)
                                {
                                    Rolse += @"{""id"":" + dv2[k]["ID"].ToString() + @",""text"":""" + dv2[k]["NAME"].ToString() + @"""" + @",""areid"":""" + dvch[j]["id"].ToInt() + @""",""value"":""" + dv2[k]["ID"].ToString() + @"""},";
                                }
                                nArea += "{" + tempnArea + @",""children"":[" + Rolse.TrimEnd(',') + "]},";
                            }


                        }


                        if (pnode != "")
                        {
                            pnode += "," + @"""children"":[" + nArea.TrimEnd(',') + "]}";

                            if (topRolse.TrimEnd(',') != "")
                                pnode += "," + topRolse.TrimEnd(',');
                        }
                        else
                            pnode = nArea.TrimEnd(',');
                      


                        context.Response.Write("[" + pnode + "]");



                    }//dv
                   
                  

                }//end else if
            }
          
        }
        /// <summary>
        /// 删除
        /// </summary>
        void DeleteItem()
        { 
           var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();

            if (lu != null)
            {
                msg.IsSuccess = 0;
                int userid = context.Request.Params["userid"].ToInt(0);
                if (lu.R_POWER == 1)
                {
                    try
                    {
                        User u = new User();
                        if (u.DeleteUserItem(userid) > 0)
                        {
                            msg.IsSuccess = 1;
                            msg.Msg = "删除成功";
                        }
                        else
                        {
                            msg.Msg = "删除失败！";
                        }
                    }
                    catch (Exception er)
                    {
                        msg.Msg = er.Message;
                    }
                }
                else
                {
                    msg.Msg = "你没权限操作！";
                }
                context.Response.Write(JsonConvert.SerializeObject(msg));
            }
        }
        /// <summary>
        /// 审核
        /// </summary>
        void AuditItem()
        { 
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
           
            if (lu != null)
            {
                msg.IsSuccess = 0;
                int userid = context.Request.Params["userid"].ToInt(0);
                if (lu.R_POWER == 1)
                {
                    try
                    {
                        User u = new User();
                        if (u.UpdateUserStates(userid, 0) > 0)
                        {
                            msg.IsSuccess = 1;
                            msg.Msg = "审核成功";
                        }
                        else
                        {
                            msg.Msg = "审核失败！";
                        }
                    }
                    catch (Exception er) {
                        msg.Msg = er.Message;
                    }
                }
                else
                {
                    
                    msg.Msg = "你没权限操作！";
                
                }
                context.Response.Write(JsonConvert.SerializeObject(msg));
            }
           
        }
        /// <summary>
        /// 审核信息列表
        /// </summary>
        void RegAuditList()
        {
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                User u = new User();
            
              
                PageView pageInfo = new PageView();
                pageInfo.PageIndex = context.Request.Params["page"].ToInt(1);
                pageInfo.PageSize = context.Request.Params["pagesize"].ToInt(20);
                pageInfo.SortName = context.Request.Params["sortname"].ToStr();
                pageInfo.SortOrder = context.Request.Params["sortorder"].ToStr();
                pageInfo.SearchParams = context.Request.Params;
                PageList<object> List = u.GetTheRegUserListOne(lu.A_LEVEL,lu.A_ID,pageInfo);

                string Rjson = @"{""Rows"":[],""Total"":""0""}";
                if (List.Data != null)
                {
                    Rjson = @"{""Rows"":" + List.Data.ToJson() + @",""Total"":""" + List.Total + @"""}";
                }
             

                context.Response.Write(Rjson);
             
            }
        }


        void isLogin()
        {
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                msg.IsSuccess = 1;
                msg.Data = new { power = lu.R_POWER,userid=lu.M_ID };

            }
            else {
                msg.IsSuccess = -9;
                msg.Msg = "登陆失效，请重新登陆!";
            }
            context.Response.Write(JsonConvert.SerializeObject(msg));
        }
        loginInfo chklogin()
        {
            try
            {
                return SessionShard.loginUser;
            }
            catch {
                msg.IsSuccess = -9;
                msg.Msg = "登陆失效，请重新登陆!";
                return null;
            }
        }

        //  取得用户的登陆信息
        void GetUserInfro()
        {
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                string v = "{ \"userid\":\""+lu.M_ID+"\", \"name\":\"" + lu.M_NAME + "\" ,  \"are\":\"" + lu.A_NAME + "\" , \"role\":\"" + lu.R_NAME + "\" }";
                context.Response.Write(v);
            }
        }

        // 退出
        void LoginOut()
        {
            var context = System.Web.HttpContext.Current;

            SessionShard.loginUser = null;

            msg.IsSuccess = 1;
            msg.Msg = "";
            context.Response.Write(JsonConvert.SerializeObject(msg));
        }

        //  取得系统设置
        void GetSystem()
        { 
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                SetSystem ss = new SetSystem();
                context.Response.Write( JsonConvert.SerializeObject( ss.GetSystem ) );
            }
        }

        // 设置系统设置
        void SetSystem()
        {
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                sysinformation si = new sysinformation();
                si.adminid = context.Request.Params["adminid"].ToInt();
                si.issend = context.Request.Params["issend"].ToBool();
                si.localhost = context.Request.Params["localhost"].ToStr();
                si.phone = context.Request.Params["phone"].ToStr();
                si.send_email = context.Request.Params["send_email"].ToStr();
                si.smtp_host = context.Request.Params["smtp_host"].ToStr();
                si.smtp_password = context.Request.Params["smtp_password"].ToStr();
                si.smtp_port = context.Request.Params["smtp_port"].ToInt();

                SetSystem ss = new SetSystem();
                ss.UpdateSystem(si);

                msg.IsSuccess = 1;
                msg.Msg = "";
                context.Response.Write(JsonConvert.SerializeObject(msg));
            }
        }

        // 删除用户资料
        void DeleteUser()
        {
            msg.IsSuccess = 0;
            var context = System.Web.HttpContext.Current;
            loginInfo lu = chklogin();
            if (lu != null)
            {
                int userid = context.Request.Params["userid"].ToInt();

                User u = new User();
                msg.IsSuccess = u.DeleteUserItem(userid);
            }
            context.Response.Write(JsonConvert.SerializeObject(msg));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}